//
//  RecevieListVC.m
//  BestLearning
//
//  Created by 星道 on 17/9/18.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "RecevieListVC.h"
#import "RecevieListCell.h"
#import "RecevieListModel.h"
#import "RecevieDetailListVC.h"
#define ScaleW (kScreenWidth / 320.f)
@interface RecevieListVC ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *_lineConstact;
@property (strong, nonatomic) IBOutlet UITableView *tableView;

/**
 数据存放
 */
@property (nonatomic, strong) NSMutableArray *DataArr;
/** 星币数据数组 */
@property (nonatomic, strong) NSMutableArray *coinsArr;
/** 当前显示的是不是红花 */
@property (nonatomic, assign) BOOL isFlower;
@property (nonatomic, strong) UIButton *TempButton;
@end

@implementation RecevieListVC


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = @"收入明细";

    self.TempButton = (UIButton *)[self.view viewWithTag:1100];

    self.isFlower = YES;

    [self RefreshData];

    [self.tableView registerNib:[UINib nibWithNibName:[RecevieListCell description] bundle:nil] forCellReuseIdentifier:@"cell"];
    self.tableView.tableHeaderView = [UIView new];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.rowHeight = 55*ScaleW;
}




#pragma mark - 网络请求
/**
 网络请求
 */
- (void)RefreshData
{

    NSDictionary *Params = @{
                             @"action"          :self.isFlower ? @"flowerRecord":@"moneyRecord",
                             @"uid"             :[AccountTool shareAccountTool].currentAccount.uid,
                             @"pageNumber"      :@1
                             };
    
    
    [NetManager Get:LLBaseURLs(@"user.do") Auth:YES Params:Params Success:^(id Obj) {
        
        //判断是否加载成功
        if ([Obj[@"code"] isEqualToNumber:@1000])
        {
            
            NSArray *Arr = Obj[@"t"][@"dataList"];
            
            // 转对象
            [Arr enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                // 模型转对象
                RecevieListModel *model = [RecevieListModel HNObjectValuesForKey:obj];
                if (self.isFlower) {
                    
                    [self.DataArr addObject:model];
                    model.num = self.DataArr.count;
                    model.isFlower = YES;
                } else {
                    
                    [self.coinsArr addObject:model];
                    model.num = self.coinsArr.count;
                    model.isFlower = NO;
                }
                
            }];
            
            [self.tableView reloadData];
        }
        else
        {
            //显示错误信息
            [MBProgressHUD showError:Obj[@"msg"]];
        }
        
        
    } Fail:^(NSError *Error) {
        //显示错误信息
        [MBProgressHUD showError:@"链接超时"];
    }];
    
    
   
}





#pragma mark - 红花、星币切换
- (IBAction)ClickFlowerOrCoins:(UIButton *)sender {

    if (self.TempButton == sender) return;


    self.TempButton.selected = !self.TempButton.selected;
    self.TempButton = sender;
    self.TempButton.selected = !self.TempButton.selected;

    if (sender.tag == 1100) {
        // 点了红花
        self.isFlower = YES;

    } else {
        // 点了星币
        self.isFlower = NO;
    }

    [self.DataArr removeAllObjects];
    [self.coinsArr removeAllObjects];

    // 立马刷新，防止点到错
    [self.tableView reloadData];

    [self RefreshData];

    // 线条
    [UIView animateWithDuration:0.2 animations:^{

        self._lineConstact.constant = self.isFlower ? 0:SCREEN_WIDTH/2;
        [self.view layoutIfNeeded];
    }];
}







#pragma mark - TableView代理
//区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.isFlower ? self.DataArr.count:self.coinsArr.count;

}

//Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecevieListModel *model;
    if (self.isFlower) {
        model = self.DataArr[indexPath.row];
    } else {
        model = self.coinsArr[indexPath.row];
    }
    RecevieListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = model;
    return cell;
}

// 点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    RecevieDetailListVC *VC = [[RecevieDetailListVC alloc] init];
    RecevieListModel *model;
    if (self.isFlower) {
        model = self.DataArr[indexPath.row];
        VC.mode = RecevieDetailListFlower;
    } else {
        model = self.coinsArr[indexPath.row];
        VC.mode = RecevieDetailListCoins;
    }
    VC.type = [model.type integerValue];
    [self.navigationController pushViewController:VC animated:YES];


}






#pragma mark - Setter
- (NSMutableArray *)DataArr
{
    if (!_DataArr)
    {
        _DataArr = [NSMutableArray array];
    }
    return _DataArr;
}

- (NSMutableArray *)coinsArr {

    if (!_coinsArr) {
        
        _coinsArr = [NSMutableArray array];
    }
    return _coinsArr;
}

@end

